<?php


namespace api\modules\v1\controllers\clinic;

use services\common\CommonService;
use Yii;
use common\models\clinic\Clinic;
use api\controllers\OnAuthController;

class TreatmentManageController extends OnAuthController
{

    public $modelClass = [];

    protected $optional = ['report-excel'];
    /**
     * @api {get} /v1/clinic/treatment-manage/index 所有用户治疗列表
     * @apiName index
     * @apiGroup treatment-manage
     *
     * @apiParam {Number} clinic_project_id                                治疗项目id
     * @apiParam {Number} clinic_id                                        诊室id
     * @apiParam {Number} page                                             页码
     * @apiParam {Number} pageSize                                         每页显示数
     * @apiParam {String} start_time                                       开始时间
     * @apiParam {String} end_time                                         结束时间
     *
     * @apiSuccess {Object}  data                                           返回数据
     * @apiSuccess {Number}  data.list.page                                 当前页码
     * @apiSuccess {Number}  data.list.pageSize                             每页显示数
     * @apiSuccess {Number}  data.list.total                                总数
     * @apiSuccess {Array}   data.list.list                                 列表
     * @apiSuccess {Number}  data.list.list.id                              ID
     * @apiSuccess {Number}  data.list.list.name                            患者名称
     * @apiSuccess {Number}  data.list.list.age                             患者年龄
     * @apiSuccess {Number}  data.list.list.gender                          性别
     * @apiSuccess {String}  data.list.list.clinic_project_name             诊疗项目
     * @apiSuccess {String}  data.list.list.patient_card_id                 患者磁卡号
     * @apiSuccess {String}  data.list.clinic                               诊室列表
     * @apiSuccess {String}  data.list.clinicProject                        治疗项目列表
     */
    // 旧方法,逻辑修改后,完全替换
//    public function actionIndexList()
//    {
//        $params = Yii::$app->request->get();
//        $page = $params['page'] ?? 1;
//        $pageSize = $params['pageSize'] ?? 10;
//        $params['project_id'] = $this->projectId;
//        $list = Yii::$app->services->clinicTreatmentManage->getList($params,$page,$pageSize);
//        //诊室筛选框
//        $condition['select'] = 'id as value,name as title';
//        $condition['status'] = Clinic::STATUS_ENABLE;
//        $condition['project_id'] = $this->projectId;
//        $clinic = Yii::$app->services->clinic->findList($condition);
//        //治疗项目筛选框
//        $clinicProject = Yii::$app->services->clinicProject->findList($condition);
//        return [
//            'list' => $list,
//            'clinic' => $clinic,
//            'clinicProject' => $clinicProject,
//        ];
//    }

    public function actionIndexList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $params['project_id'] = $this->projectId;
        $list = Yii::$app->services->clinicTreatmentManage->getList($params, $page, $pageSize);

        return $list;
    }

    /**
     * 治疗列表左侧 治疗室-治疗项目列表
     * @api {get} /v1/clinic/treatment-manage/clinic-project
     * @apiName clinic-project
     * @apiGroup treatment-manage
     *
     */
    public function actionClinicProject()
    {
        return Yii::$app->services->clinicTreatmentManage->treatmentRoomList($this->projectId);
    }

    /**
     * @apiName 诊室列表
     * @apiGroup
     * @author loujie
     */
    public function actionClinicList()
    {
      return CommonService::getClinicList();
    }


    /**
     * 录入治疗列表
     * @api {get} /v1/clinic/treatment-manage/patient-clinic-list
     * @apiName patient-clinic-list
     * @apiGroup treatment-manage
     *
     * @apiParam {Number} clinic_project_id                                治疗项目id
     * @apiParam {String} keywords                                         搜索关键词
     *
     */
    public function actionPatientClinicList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $list = Yii::$app->services->clinicTreatmentManage->patientClinicList($params);

        return $list;
    }

    /**
     * 录入治疗信息
     * @api {POST} /v1/clinic/treatment-manage/add-treatment
     * @apiName add-treatment
     * @apiGroup treatment-manage
     *
     * @apiParam (String) time 治疗时间
     * @apiParam (Number) template 模板类型(在添加治疗项目时配置)
     * @apiParam (String) content 308模板专用字段 [{"body_parts_large_name":5,"body_parts_small_name":2,"num":1,"dose":1}]
     * @apiParam (Number) patient_card_id 诊疗号id
     * @apiParam (Number) clinic_project_id 治疗项目id
     * @apiParam (Number) treatment_room_id 治疗室id
     * @apiParam (String) body_parts_large_name 部位
     * @apiParam (Number) num 数量
     * @apiParam (String) dose
     * @apiParam (String) remark 备注
     */
    public function actionAddTreatment()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        return Yii::$app->services->clinicTreatmentManage->addTreatment($params);
    }

    /**
     * 这个人往期诊疗记录
     * @api {GET} /v1/clinic/treatment-manage/treatment-record
     * @apiName treatment-record
     * @apiGroup treatment-manage
     *
     * @apiParam (Number) patient_card_id    患者诊疗号
     * @apiParam (Number) clinic_project_id  诊疗项目di
     * @apiParam (Number) page               页数
     * @apiParam (Number) pageSize           每页的页码
     */
    public function actionTreatmentRecord()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        return Yii::$app->services->clinicTreatmentManage->treatmentRecord($params, $page, $pageSize);
    }

    /**
     * 更新治疗信息
     * @api {POST} /v1/clinic/treatment-manage/update-treatment
     * @apiName update-treatment
     * @apiGroup treatment-manage
     *
     * @apiParam (String) time 治疗时间
     * @apiParam (Number) template 模板类型(在添加治疗项目时配置)
     * @apiParam (String) content 308模板专用字段 [{"body_parts_large_name":5,"body_parts_small_name":2,"num":1,"dose":1}]
     * @apiParam (Number) id 治疗id
     * @apiParam (String) body_parts_large_name 部位
     * @apiParam (Number) num 数量
     * @apiParam (String) dose
     * @apiParam (String) remark 备注
     */
    public function actionUpdateTreatment()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        return Yii::$app->services->clinicTreatmentManage->UpdateTreatment($params);
    }


    /**
     * @api {get} /v1/clinic/treatment-manage/report 治疗报表【诊室】
     * @apiName report
     * @apiGroup treatment-manage
     *
     * @apiParam {String} start_time                                        开始时间【格式：2020-01-01】
     * @apiParam {String} end_time                                          结束时间【格式：2020-01-01】
     *
     * @apiSuccess {Object}  data                                           返回数据
     * @apiSuccess {Number}  data.all_treament_patient_num                  总治疗人数
     * @apiSuccess {Number}  data.tmp_treament_patient_num                  308治疗人数
     * @apiSuccess {Number}  data.tmp_buy_num                               308购买量
     * @apiSuccess {Number}  data.tmp_treament_num                          308治疗量
     * @apiSuccess {Array}   data.list                                      诊室数据
     * @apiSuccess {String}  data.list.clinic_project_name                  治疗项目名称
     * @apiSuccess {String}  data.list.clinic_name                          诊室名
     * @apiSuccess {Number}  data.list.treament_clinic_num                  治疗量
     * @apiSuccess {Number}  data.list.treament_patient_num                 治疗人数
     * @apiSuccess {Number}  data.list.treament_patient_times_num           治疗人次
     * @apiSuccess {Number}  data.list.treament_rate                        治疗频率
     * @apiSuccess {Number}  data.list.treament_use_num                     人均用量
     * @apiSuccess {Number}  data.list.buy_clinic_num                       购买量
     * @apiSuccess {Number}  data.list.buy_patient_num                      购买人数
     * @apiSuccess {Number}  data.list.buy_use_num                          人均购买
     * @apiSuccess {Array}  data.list.children                              子数据【同上】
     */
    public function actionReport()
    {
        $params = Yii::$app->request->get();
        $params['start_time'] = $params['start_time'] ?? '';
        $params['end_time'] = $params['end_time'] ?? '';
        $params['project_id'] = $this->projectId;

        // return Yii::$app->services->clinicTreatment->report($params);
        return Yii::$app->services->clinicUserTreatment->report($params);
    }

    /**
     * 导出治疗统计excel 参数同上
     */
    public function actionReportExcel()
    {
        $params = Yii::$app->request->get();
        $params['start_time'] = $params['start_time'] ?? '';
        $params['end_time'] = $params['end_time'] ?? '';
//        $params['project_id'] = $this->projectId;

        // return Yii::$app->services->clinicTreatment->report($params);
        return Yii::$app->services->clinicUserTreatment->reportExcel($params);
    }

    /**
     * @api {get} /v1/clinic/treatment-manage/project-report 治疗报表【项目】
     * @apiName report
     * @apiGroup treatment-manage
     *
     * @apiParam {String} start_time                                        开始时间【格式：2020-01-01】
     * @apiParam {String} end_time                                          结束时间【格式：2020-01-01】
     *
     * @apiSuccess {Object}  data                                           返回数据
     * @apiSuccess {Number}  data.all_treament_patient_num                  总治疗人数
     * @apiSuccess {Number}  data.tmp_treament_patient_num                  308治疗人数
     * @apiSuccess {Number}  data.tmp_buy_num                               308购买量
     * @apiSuccess {Number}  data.tmp_treament_num                          308治疗量
     * @apiSuccess {Array}   data.list                                      诊室数据
     * @apiSuccess {String}  data.list.clinic_project_name                  治疗项目名称
     * @apiSuccess {String}  data.list.pname                                项目名
     * @apiSuccess {Number}  data.list.treament_clinic_num                  治疗量
     * @apiSuccess {Number}  data.list.treament_patient_num                 治疗人数
     * @apiSuccess {Number}  data.list.treament_patient_times_num           治疗人次
     * @apiSuccess {Number}  data.list.treament_rate                        治疗频率
     * @apiSuccess {Number}  data.list.treament_use_num                     人均用量
     * @apiSuccess {Number}  data.list.buy_clinic_num                       购买量
     * @apiSuccess {Number}  data.list.buy_patient_num                      购买人数
     * @apiSuccess {Number}  data.list.buy_use_num                          人均购买
     * @apiSuccess {Array}  data.list.children                              子数据【同上】
     */
    public function actionProjectReport()
    {
        $params = Yii::$app->request->get();
        $params['start_time'] = $params['start_time'] ?? '';
        $params['end_time'] = $params['end_time'] ?? '';

        return Yii::$app->services->clinicUserTreatment->projectReport($params);
    }

    /**
     * @api {get} /v1/clinic/treatment-manage/view 单用户治疗列表
     * @apiName view
     * @apiGroup treatment-manage
     * @apiParam {Number} id                                  治疗id
     *
     * 以下废弃
     * @apiParam {Number} patient_card_id                                  用户磁卡id
     * @apiParam {Number} page                                             页码
     * @apiParam {Number} pageSize                                         每页显示数
     * @apiParam {String} start_time                                       开始时间
     * @apiParam {String} end_time                                         结束时间
     *
     */
//    public function actionViewTreatment() 废弃
//    {
//        $params = Yii::$app->request->get();
//        $page = $params['page'] ?? 1;
//        $pageSize = $params['pageSize'] ?? 10;
//        $params['project_id'] = $this->projectId;
//        $list = Yii::$app->services->clinicTreatmentManage->treatmentList($params,$page,$pageSize);
//
//        $clinicProjectTotal = [];
//        if ($page == 1) {
//            $clinicProjectTotal = Yii::$app->services->clinicTreatmentManage->clinicTotal($params['patient_card_id']);
//        }
//        //诊室筛选框
//        $condition['select'] = 'id as value,name as title';
//        $condition['status'] = Clinic::STATUS_ENABLE;
//        $condition['project_id'] = $this->projectId;
//        //治疗项目筛选框
//        $clinicProject = Yii::$app->services->clinicProject->findList($condition);
//        return [
//            'list' => $list,
//            'clinicProject' => $clinicProject,
//            'clinicProjectTotal' => $clinicProjectTotal,
//        ];
//    }

    public function actionViewTreatment()
    {
        $params = Yii::$app->request->get();

        $params['project_id'] = $this->projectId;
        $list = Yii::$app->services->clinicTreatmentManage->viewTreatment($params);
        return $list;

    }

    /**
     * @api {get} /v1/clinic/treatment-manage/treatment-detail 治疗详情
     * @apiName index
     * @apiGroup treatment-manage
     *
     * @apiParam {Number} id                                用户治疗id
     *
     */
    public function actionTreatmentDetail()
    {
        $id = Yii::$app->request->get('id');
        return Yii::$app->services->clinicTreatmentManage->treatmentDetail($id);
    }

    /**
     * @api {get} /v1/clinic/treatment-manage/operator-List 操作员列表
     * @apiName operator-List
     * @apiGroup treatment-manage
     *
     */
    public function actionOperatorList()
    {
        return Yii::$app->services->clinicTreatmentManage->operatorList($this->projectId);
    }

    public function actionTreatmentPart()
    {
        return Yii::$app->services->clinicTreatmentManage->treatmentPart();
    }

    /**
     * @api {post} /v1/clinic/treatment-manage/update-clinic-num
     * @apiName
     * @apiGroup
     * @apiParam {Number} total_num  治疗项目总数
     * @apiParam {Number} help_num  援助总数
     * @apiParam {String} patient_card_id 诊疗号
     * @apiParam {Number} clinic_project_id 治疗项目id
     * @author loujie
     * @return bool
     */
    public function actionUpdateClinicNum()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        return Yii::$app->services->clinicTreatmentManage->updateClinicNum($params);
    }
}
